package com.gqj.bas.controller;

import com.gqj.bas.domain.BasSort;
import com.gqj.bas.service.IBasSortService;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资产分类Controller
 * 
 * @author lxq
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/bas/basSort")
public class BasSortController extends BaseController
{
    @Autowired
    private IBasSortService basSortService;

    /**
     * 查询资产分类列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basSort:list')")
    @GetMapping("/list")
    public AjaxResult list(BasSort basSort)
    {
        basSort.setDelFlag("0");
        List<BasSort> list = basSortService.selectBasSortList(basSort);
        return AjaxResult.success(list);
    }

    /**
     * 导出资产分类列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basSort:export')")
    @Log(title = "资产分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasSort basSort)
    {
        List<BasSort> list = basSortService.selectBasSortList(basSort);
        ExcelUtil<BasSort> util = new ExcelUtil<BasSort>(BasSort.class);
        util.exportExcel(response, list, "资产分类数据");
    }

    /**
     * 获取资产分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basSort:query')")
    @GetMapping(value = "/{sortId}")
    public AjaxResult getInfo(@PathVariable("sortId") Long sortId)
    {
        return AjaxResult.success(basSortService.selectBasSortBySortId(sortId));
    }

    /**
     * 新增资产分类
     */
    @PreAuthorize("@ss.hasPermi('bas:basSort:add')")
    @Log(title = "资产分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasSort basSort)
    {
        if (UserConstants.NOT_UNIQUE.equals(basSortService.checkSortNameUnique(basSort)))
        {
            return AjaxResult.error("新增分类'" + basSort.getSortName() + "'失败，分类名称已存在");
        }
        basSort.setCreateBy(getUsername());
        return toAjax(basSortService.insertBasSort(basSort));
    }

    /**
     * 修改资产分类
     */
    @PreAuthorize("@ss.hasPermi('bas:basSort:edit')")
    @Log(title = "资产分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasSort basSort)
    {

        if (UserConstants.NOT_UNIQUE.equals(basSortService.checkSortNameUnique(basSort)))
        {
            return AjaxResult.error("修改分类'" + basSort.getSortName() + "'失败，分类名称已存在");
        }
        else if (basSort.getParentId().equals(basSort))
        {
            return AjaxResult.error("修改分类'" + basSort.getSortName() + "'失败，上级分类不能是自己");
        }
        basSort.setUpdateBy(getUsername());
        return toAjax(basSortService.updateBasSort(basSort));
    }

    /**
     * 删除资产分类
     */
    @PreAuthorize("@ss.hasPermi('bas:basSort:remove')")
    @Log(title = "资产分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sortId}")
    public AjaxResult remove(@PathVariable Long sortId)
    {
        if (basSortService.hasChildBySortId(sortId))
        {
            return AjaxResult.error("存在下级部门,不允许删除");
        }
        if (basSortService.checkMaterialExistSort(sortId))
        {
            return AjaxResult.error("分类已被引用,不允许删除");
        }
        return toAjax(basSortService.deleteBasSortBySortId(sortId));
    }
}
